import { serve } from "bun";
import { withCorsForRoutes, withCors } from "./middleware/cors";
import { routes } from "./routes";
import config from "./config/env";

// 应用 CORS 中间件到所有路由
const corsRoutes = withCorsForRoutes(routes);

const server = serve({
    port: config.server.port,
    hostname: config.server.hostname,
  
    // `routes` requires Bun v1.2.3+
    routes: corsRoutes,
  
    // (optional) fallback for unmatched routes:
    // Required if Bun's version < 1.2.3
    fetch: withCors(async (req) => {
      console.log("🔄 Fallback 处理:", req.url);
      return new Response("Bun 服务器运行中", { status: 200 });
    }),
  });

console.log(`🚀 服务器运行在 http://${server.hostname}:${server.port}`);